
"""
    局部 赋值（这是默认状态）不会改变 scope_test 对 spam 的绑定。
    nonlocal 赋值会改变 scope_test 对 spam 的绑定，而 global 赋值会改变模块层级的绑定
"""


def scope_test():
    def do_local():
        spam = "local spam"

    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"

    def do_global():
        global spam
        spam = "global spam"

    spam = "test_dir spam"
    do_local()
    print("After local assigment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

    do_local()
    print("After local assigment:", spam)


scope_test()
print("In global scope:", spam)

